﻿
using System;
using System.Collections;
using System.Configuration;
using System.Drawing;

namespace mlnx_os_sysdump_analyzer
{
	/// <summary>
	/// Description of MarkerList.
	/// </summary>
	[Serializable]
	public class MarkerList
	{
		/// <summary>
		/// list of markers
		/// </summary>
		private System.Collections.Generic.List<Marker> markers = null;
		private bool _clearedByUser;

        //----------------------------------------------------
		/// <summary>
		/// constructor, init the list
		/// </summary>
		public MarkerList()
		{
			markers = new System.Collections.Generic.List<Marker>();
			_clearedByUser = false;
		}
        //----------------------------------------------------

		/// <summary>
		/// get an Enumerator
		/// </summary>
		public System.Collections.IEnumerator MarkersEnumerator
	    {
	        get
	        {
	        	return markers.GetEnumerator();
	        }
	    }
        //----------------------------------------------------

		/// <summary>
		/// add a marker to the list 
		/// </summary>
		/// <param name="keyword"></param>
		/// <param name="color"></param>
		public bool addMarker(string keyword, Color color)
		{
			Marker item = new Marker(keyword, color);
			_clearedByUser = false;
			// check if the marker already exists in the list 
			if (markers.Contains(item))
			{
				return false;
			}
			else
			{
				markers.Add(item);
				return true;
			}
		}
        //----------------------------------------------------

		/// <summary>
		/// remove a marker from the list 
		/// </summary>
		/// <param name="keyword"></param>
		/// <param name="color"></param>
		public void removeMarker(string keyword, Color color)
		{
			Marker item = new Marker(keyword, color);
			// check if the marker already exists in the list 
			if (markers.Contains(item)) 
			{
				markers.Remove(item);
			}
			if (markers.Count == 0)
			{
				_clearedByUser = true;
			}
		}
        //----------------------------------------------------

		/// <summary>
		/// clear the list of markers
		/// </summary>
		public void clearMarkers()
		{
			markers.Clear();
			_clearedByUser = true;
		}
        //----------------------------------------------------

		/// <summary>
		/// get number of configured markers
		/// </summary>
		/// <returns></returns>
		public int getCount()
		{
			return markers.Count;
		}
        //----------------------------------------------------

		/// <summary>
		/// check whether the user cleared the markers. if so don't init the db
		/// </summary>
		/// <returns></returns>
		public bool clearedByUser()
		{
			return _clearedByUser;
		}
        //----------------------------------------------------
		
		 // set dafault markers only if the user didn't clear them
        public static void InitMarkers()
        {
        	try
        	{
	        	MarkerListSett markerSettings = new MarkerListSett();     
	        	markerSettings.Reload();
	        	
	        	if (markerSettings.Marker_List.getCount() == 0 && !markerSettings.Marker_List.clearedByUser()) 
	            {
	        		MarkerList tmp = MarkerList.GetDefaultMarkersList();
		        	IEnumerator ienum = tmp.MarkersEnumerator;
		           	while(ienum.MoveNext())
			        {
		        		Marker marker1 = (Marker)ienum.Current;
		        		markerSettings.Marker_List.addMarker(marker1.keywork, marker1.markerColor);
			        }
		           	markerSettings.Save();
	            }
        	}
        	catch{}
        }
        //----------------------------------------------------

        /// <summary>
        /// force setting the markers to default
        /// </summary>
        public static MarkerList GetDefaultMarkersList()
        {
        	try
        	{
        		MarkerList tmp = new MarkerList();
	        	// add default markers
	            tmp.addMarker("Linux", System.Drawing.Color.Lime);
	        	tmp.addMarker(".ERR", System.Drawing.Color.Red);
	            tmp.addMarker("user admin: Executing", System.Drawing.Color.Yellow);
	            
	            return tmp;
        	}
        	catch
        	{
        		return null;
        	}
        }
        //----------------------------------------------------
        
        public static void ClearMarkers()
        {
        	try
        	{
	        	MarkerListSett markerSettings = new MarkerListSett();     
	        	markerSettings.Reload();
	            
	        	markerSettings.Marker_List.clearMarkers(); 
	        	markerSettings.Save();
        	}
        	catch{}
        }
        //----------------------------------------------------
	}
	
    //
    // for saving the marker list settings on the computer
    //
	internal sealed class MarkerListSett : ApplicationSettingsBase
		{
		    // If you used [ApplicationScopedSetting()] instead of [UserScopedSetting()],
		    // you would NOT be able to persist any data changes!
		    [UserScopedSetting()]
		    [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Binary)]
		    [DefaultSettingValue("")]
		    public MarkerList Marker_List
		    {
		        get
		        {
		            return ((MarkerList)this["Marker_List"]);
		        }
		        set
		        {
		            this["Marker_List"] = (MarkerList)value;
		        }
		    }
		}
    //----------------------------------------------------
}
